<?php


namespace app\app\controller;


use app\app\model\BannerModel;
use app\app\validate\BannerValidate;
use cmf\controller\AdminBaseController;

class BannerController extends AdminBaseController
{
    //编辑页面
    public function edit()
    {
        $id = input('id');
        if (!$id){
            $this->error('错误');
        }
        $banner = BannerModel::find($id);
        if (!$banner) {
            $this->error('错误');
        }
        return $this->fetch('edit',[
            'Banner' => $banner
        ]);

    }
    //编辑功能
    public function editPost()
    {
        $data = input('post.');
        $validate = new BannerValidate();
        //验证器
        $res = $validate->scene('add')->check($data);
        if (!$res) {
            $this->error($validate->getError());
        }
        $Banner = BannerModel::find($data['id']);
        if (!$Banner) {
            $this->error('错误');
        }
        $Banner->image = $data['image'];
        $Banner->url = $data['url'];
        $Banner->update_time = date('Y-m-d H:i:s');
        if ($Banner->save()) {
            $this->success('保存成功','Banner/index');
        }else{
            $this->error('保存失败');
        }
    }
    //轮播图
    public function index()
    {
        $data = BannerModel::select();
        return $this->fetch('index',[
            'Banner' => $data
        ]);
    }
    //添加
    public function add()
    {
        return $this->fetch();
    }
    //添加
    public function addPost()
    {
        $data = input('post.');
        $validate = new BannerValidate();
        $res = $validate->scene('add')->check($data);
        if (!$res) {
            $this->error($validate->getError());
        }
        $data['create_time'] = date('Y-m-d H:i:s');
        $banner = BannerModel::create($data);
        if ($banner) {
            $this->success('添加成功','Banner/index');
        }else{
            $this->error('添加失败');
        }
    }



}